#!/usr/bin/python
#coding=utf8
# C06-send -- send email.
# 自动发送邮件, 带附件

import smtplib
import datetime
import email.MIMEMultipart
import email.MIMEText
import email.MIMEBase
from email.mime.image import MIMEImage
from email.header import Header
import os.path

#####################################################################
## Part 1. 定义全局变量

today = datetime.date.today()
me = 'bestvbmm@163.com'
password = 'bmm@bestv757'
smtp = 'smtp.163.com'
#you = ['he.yongneng@bestv.com.cn']
you = ['he.yongneng@bestv.com.cn','li.benjia@bestv.com.cn','wu.yunpeng@bestv.com.cn','cao.tingting@bestv.com.cn','zhang.qi@bestv.com.cn','song.fulei@bestv.com.cn','lu.kun@bestv.com.cn','chen.yongqiang@bestv.com.cn']
mail_name = "mailcontent.html"
file_name = "/root/13-recommend/out/13-recommend.zh.pdf"
image_name = "/root/13-recommend/out/13-recommend.zh.jpg"

#####################################################################
## Part 2. 设置邮件根容器

main_msg = email.MIMEMultipart.MIMEMultipart()
main_msg['From'] = me
main_msg['To'] = ','.join(you)
main_msg['Subject'] = Header("【监控结果】%s" % today, 'utf-8')
main_msg['Date'] = email.Utils.formatdate()

#####################################################################
## Part 3. 设置邮件内容, 并附加到邮件根容器

data = open(mail_name, 'rb')
text_msg = email.MIMEText.MIMEText(data.read(), 'html', 'utf-8')
data.close()
main_msg.attach(text_msg)

#####################################################################
## Part 4. 设置图片内容, 并附加到邮件根容器

#data = open(image_name, 'rb')
#image_msg = MIMEImage(data.read())
#data.close()
#image_msg.add_header('Content-ID', '<meinv_image>')
#main_msg.attach(image_msg)

#####################################################################
## Part 4. 设置邮件附件, 并附加到邮件根容器

#file_msg = email.MIMEBase.MIMEBase('application', 'octet-stream')
#file_msg.add_header('Content-Disposition', 'attachment', filename = os.path.basename(file_name))
#data = open(file_name, 'rb')
#file_msg.set_payload(data.read())
#data.close()
#email.Encoders.encode_base64(file_msg)
#main_msg.attach(file_msg)

#####################################################################
## Part 5. 发送邮件

server = smtplib.SMTP(smtp)
server.login(me, password)
try:
    server.sendmail(me, you, main_msg.as_string())
finally:
    server.quit()
